import jade.core.Agent;
import jade.core.behaviours.*;

import java.util.Dictionary;
import java.util.Vector;
import java.io.*;
import java.util.*;

import jade.core.behaviours.*;
import jade.lang.acl.*;

public class AgentRecunoastere extends Agent{

	protected static String assertTemplate="(assert (do (id %d) (persoana %s)(ora %s) (minut %s) (actiune %s)(ap %s)))";
	protected static int id = 1;
	protected static Map dic = new HashMap<String, Integer>();
	
	protected void setup()
	{
		/*
		addBehaviour(new CyclicBehaviour(this){
			
			public void action()
			{
				block(100);
				if(!BlackBoard.queue.isEmpty())
				{
					WriteFile.write("test", BlackBoard._TXT_PATH+"log.txt");
					String linie=BlackBoard.get();
					process(linie);
				}
			}
	});*/
		while(true)
		{
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//WriteFile.write("test", BlackBoard._TXT_PATH+"log.txt");
			if(!BlackBoard.queue.isEmpty())
			{
				WriteFile.write("test not empty", BlackBoard._TXT_PATH+"log.txt");
				String linie=BlackBoard.get();
				process(linie);
			}
		}
}
	protected void process(String linie)
	{
		if(actionConsumesEnergy(linie))
		{
			String[]  slinie=linie.split(" ");
			String subiect = slinie[0];
			if(linie.indexOf("from")!=-1)
			{//create 2 assert rules
				String oraFrom="";
				String oraTo="";
				String minFrom="";
				String minTo="";
				String actiune1="start";
				String actiune2="stop";
				String aparat="";
				for(int i=1;i<slinie.length;i++)
				{
					if(slinie[i].equals("from"))
					{
						oraFrom=slinie[i+1].split(":")[0];
						minFrom=slinie[i+1].split(":")[1];
					}
					if(slinie[i].equals("to"))
					{
						oraTo=slinie[i+1].split(":")[0];
						minTo=slinie[i+1].split(":")[1];
					}
				}
				aparat=getAparat(linie);
				String assert1=String.format(assertTemplate, id, subiect, oraFrom, minFrom, actiune1, aparat );
				id++;
				String assert2=String.format(assertTemplate, id, subiect, oraTo, minTo, actiune2, aparat );
				id++;
				WriteFile.write(assert1, "C:/Documents and Settings/Administrator/Desktop/mas/ProiectMAS/src/testJess.clp");
				WriteFile.write(assert2, "C:/Documents and Settings/Administrator/Desktop/mas/ProiectMAS/src/testJess.clp");
			}
			else
			{//are at
				String actiune= IsStartAction(linie)? "start":"stop";
				String aparat=getAparat(linie);
				int localId=0;
				if(actiune.equals("start"))
				{
					dic.put(aparat, id);
					localId = id;
				}
				else
				{
					String s = (dic.get(aparat)).toString();
					localId = Integer.parseInt(s);
					dic.remove(aparat);
				}
				String ora="";
				String minut="";
				for(int i=1;i<slinie.length;i++)
				{
					if(slinie[i].equals("at"))
					{
						ora=slinie[i+1].split(":")[0];
						minut=slinie[i+1].split(":")[1];
					}
				}
				String assertStatement=String.format(assertTemplate, localId, subiect, ora, minut, actiune, aparat );
				id++;
				WriteFile.write(assertStatement, "C:/Documents and Settings/Administrator/Desktop/mas/ProiectMAS/src/testJess.clp");
			}
			
		}
	}

	protected boolean actionConsumesEnergy(String linie)
	{
		return getAparat(linie) !="";
	}
	
	protected String getAparat(String linie)
	{
		Vector<String> aparate=null;
		try{
		aparate=ReadFile.read(BlackBoard._TXT_PATH+"aparateCurent.txt");
		for(int i=0;i<aparate.size();i++)
		{
			if(linie.indexOf(aparate.elementAt(i))!=-1)
			{
				return aparate.elementAt(i);
			}
		}
		}
		catch(Exception e)
		{
			WriteFile.write("Error reading aparateCurent.txt", BlackBoard._TXT_PATH+"log.txt");
		}
		return "";
	}
	
	protected boolean IsStartAction(String linie)
	{
		if(linie.indexOf("turns_on")!=-1)
		{
			return true;
		}
		return false;
	}
}
